﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Properties;
using ITAP.Core.Modelling;

namespace ITAP.ShprossesBuilder.Operations
{
    public class OperationRotateShprossConnectors : IOperation
    {
        private ModelControl modelControl;
        #region IOperation Members
        
        public string Name
        {
            get { return "Вращение соединителей"; }
        }

        public OperationGroup OperationGroup
        {
            get { return OperationGroup.ShprossConnectorOperation; }
        }

        public System.Drawing.Image Thumbnail
        {
            get { return Resources.RotateShprossConnectors.ToBitmap(); }
        }

        public void Execute(ModelControl modelControl)
        {
            this.modelControl = modelControl;
            ContextMenuStrip menuStrip = new ContextMenuStrip();

            EventHandler handler = new EventHandler(OnItemClick);
            ToolStripItem clockwise = new ToolStripMenuItem("90° по часовой стрелке", null, handler);
            clockwise.Tag = new Angle(-Math.PI / 2);
            menuStrip.Items.Add(clockwise);
            ToolStripItem antiClockwise = new ToolStripMenuItem("90° против часовой стрелки", null, handler);
            antiClockwise.Tag = new Angle(Math.PI / 2);
            menuStrip.Items.Add(antiClockwise);

            ToolStripItem pi = new ToolStripMenuItem("180°" , null, handler);
            pi.Tag = new Angle(Math.PI);
            menuStrip.Items.Add(pi);

            menuStrip.Show(Cursor.Position);             
        }

        public bool CanUndo
        {
            get { return true; }
        }

        #endregion

        private void OnItemClick(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
            this.RotateSelectedShprossConnectors(menuItem.Tag as Angle);
        }

        private void RotateSelectedShprossConnectors(Angle angle)
        {
            foreach (IComponent selectedComponent in this.modelControl.ModelContainer.SelectedComponentsStack)
            {
                if (selectedComponent is ShprossConnector)
                    (selectedComponent as ShprossConnector).Rotate(angle.Value);
            }
            this.modelControl.UpdateModel();
        }
    }
}
